package com.wmh.baseservice.common.redis.config;


import com.wmh.baseservice.common.redis.utils.RedisUtil;
import com.wmh.baseservice.common.utils.LogUtil;
import com.wmh.baseservice.merchant.repository.MerchantGroupGoodsRepository;
import com.wmh.baseservice.merchant.service.MerchantGroupGoodsService;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

import javax.annotation.PostConstruct;

@Configuration
public class KeyOverdueEvent extends KeyExpirationEventMessageListener {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private MerchantGroupGoodsRepository merchantGroupGoodsRepository;

    @Autowired
    private MerchantGroupGoodsService merchantGroupGoodsService;

    public KeyOverdueEvent(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

//    @Override
//    public void onMessage(Message message, byte[] pattern) {
//        String key = message.toString();
//        if(key.startsWith(MerchantGroupGoodsRepository.KEY_PREFIX)){
//            long id = Long.parseLong(key.split(":")[2]);
//            LogUtil.info("团购监控 -> 团购结束 id : {}" ,id);
//            // 结算失败
//            merchantGroupGoodsService.settlementGroupGoodsCashBack(id);
//        }
//    }

    @PostConstruct
    public void initEvent(){
        LogUtil.info("准备缓存预热...");
        merchantGroupGoodsRepository.init();
        LogUtil.info("缓存预热完毕...");
    }
}
